package co.yixiang.huiMin.controller;


import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.huiMin.dao.TeachingVideoQueryCriteria;
import co.yixiang.huiMin.entity.TeachingVideo;
import co.yixiang.huiMin.entity.YxStoreProduct;
import co.yixiang.huiMin.service.YxStoreProductService;
import co.yixiang.modules.shop.domain.YxUserBill;
import co.yixiang.modules.shop.repository.YxUserBillRepository;
import co.yixiang.modules.shop.service.YxUserBillService;
import co.yixiang.modules.shop.service.dto.YxUserBillQueryCriteria;
import co.yixiang.modules.system.domain.UserShop;
import co.yixiang.modules.system.service.UserShopService;
import co.yixiang.modules.system.service.dto.UserShopDto;
import co.yixiang.modules.system.service.dto.UserShopQueryCriteria;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author Author
 * @since 2020-07-04
 */

@Api(tags = "商品")
@RestController
@RequestMapping("/yxStoreProduct")
public class YxStoreProductController {


         @Resource
         private YxStoreProductService yxStoreProductService;
         @Resource
         private UserShopService userShopService;
         @Resource
         private YxUserBillService yxUserBillService;
         @Resource
         private YxUserBillRepository yxUserBillRepository;

    @ApiOperation("查询首页商户统计")
    @AnonymousAccess
    @GetMapping("selectYxStoreProductCount")
    public ResponseEntity selectYxStoreProductCount(String merId){
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("code",200);

        stringObjectHashMap.put("message",yxStoreProductService.selectYxStoreProductCount(merId));
        return ResponseEntity.ok(stringObjectHashMap);
    }

    @ApiOperation("根据商户id商户信息")
    @AnonymousAccess
    @GetMapping("getUserShopByIds")
    public ResponseEntity<Object> getUserShopByIds(UserShopQueryCriteria criteria){

        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        List<UserShopDto> map = userShopService.queryAll(criteria);

        stringObjectHashMap.put("code",200);
        stringObjectHashMap.put("message",map);
        return ResponseEntity.ok(stringObjectHashMap);

    }


    @ApiOperation("查询收支记录")
    @AnonymousAccess
    @GetMapping(value = "/yxUserBill")
    public ResponseEntity getYxUserBills(UserShopQueryCriteria criteria, Pageable pageable){
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        List<UserShopDto> list = userShopService.queryAll(criteria);

        YxUserBillQueryCriteria criteria1=new YxUserBillQueryCriteria();
        criteria1.setUid(list.get(0).getUser().getUid());
        criteria1.setTitle("商品卖出");
        Page<TeachingVideo> teachingVideos = yxUserBillRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria1, criteriaBuilder), pageable);
        stringObjectHashMap.put("message", PageUtil.toPage(teachingVideos));
        stringObjectHashMap.put("code",200);

        return new ResponseEntity(stringObjectHashMap, HttpStatus.OK);
    }

}

